package com.atguigu.lease.web.admin.controller.system;

import com.atguigu.lease.common.exception.StgyException;
import com.atguigu.lease.common.result.Result;
import com.atguigu.lease.common.result.ResultCodeEnum;
import com.atguigu.lease.model.entity.SystemUser;
import com.atguigu.lease.web.admin.dto.SystemUserSearchDto;
import com.atguigu.lease.web.admin.service.SystemUserService;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/admin/system/user")
@Tag(name = "系统管理员管理模块")
public class SystemUserController {
    @Resource
    SystemUserService systemUserService;
    ///admin/system/user/isUserNameAvailable?username=zhangsan
    @Operation(summary = "条件分页查询管理员列表")
    @GetMapping("/isUserNameAvailable")
    public Result<Object> page(@RequestParam("username")String username){
        Boolean available = !systemUserService.exists(Wrappers.lambdaQuery(SystemUser.class)
                .eq(SystemUser::getUsername , username));
        return Result.ok().data(available);

    }
    @Operation(summary = "条件分页查询管理员列表")
    @GetMapping("/page")
    public Result<Page<SystemUser>> page(SystemUserSearchDto searchDto){
        return Result.ok(systemUserService.getPage(searchDto));

    }
    @Operation(summary = "新增或更新管理员")
    @PostMapping("saveOrUpdate")
    public Result saveOrUpdate(@RequestBody SystemUser systemUser){

        return Result.build(systemUserService.saveOrUpdateSystemUser(systemUser));
    }
}
